﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using S7.Net;

namespace SimensPLC
{
    public partial class Form1 : Form
    {
        Plc siemens;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.SelectedIndex == 0)
                {
                    siemens = new Plc(CpuType.S71200, txtIP.Text, 0, 1);
                }
                if (comboBox1.SelectedIndex == 1)
                {
                    siemens = new Plc(CpuType.S71200, txtIP.Text, 0, 1);
                }
                if (comboBox1.SelectedIndex == 2)
                {
                    siemens = new Plc(CpuType.S71500, txtIP.Text, 0, 1);
                }
                // 连接对象
                siemens.Open();
                if (!siemens.IsConnected)
                {
                    labStatus.Text = "连接PLC失败！";
                }
                else
                {
                    labStatus.Text = "连接PLC成功！";
                    timer1.Enabled = true;
                }
            }
            catch
            {
                MessageBox.Show("参数错误，请检查！");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                siemens.Close();
                labStatus.Text = "PLC断开连接！";
                timer1.Enabled = false;
            }
            catch
            {
                MessageBox.Show("参数错误，请检查！");
            }
        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            #region
            if ((bool)siemens.Read("DB2.DBX0.1"))
            {
                labx1.BackColor = Color.Red;
            }
            else
            {
                labx1.BackColor = Color.Gray;
            }
            if ((bool)siemens.Read("DB2.DBX0.2"))
            {
                labx2.BackColor = Color.Red;
            }
            else
            {
                labx2.BackColor = Color.Gray;
            }
            if ((bool)siemens.Read("DB2.DBX0.3"))
            {
                labx3.BackColor = Color.Red;
            }
            else
            {
                labx3.BackColor = Color.Gray;
            }
            if ((bool)siemens.Read("DB2.DBX0.4"))
            {
                labx4.BackColor = Color.Red;
            }
            else
            {
                labx4.BackColor = Color.Gray;
            }
            #endregion

            #region
            if ((bool)siemens.Read("DB2.DBX0.5"))
            {
                btny1.BackColor = Color.Red;
            }
            else
            {
                btny1.BackColor = Color.Blue;
            }
            if ((bool)siemens.Read("DB2.DBX0.6"))
            {
                btny2.BackColor = Color.Red;
            }
            else
            {
                btny2.BackColor = Color.Blue;
            }
            if ((bool)siemens.Read("DB2.DBX0.7"))
            {
                btny3.BackColor = Color.Red;
            }
            else
            {
                btny3.BackColor = Color.Blue;
            }
            if ((bool)siemens.Read("DB2.DBX0.0"))
            {
                btny4.BackColor = Color.Red;
            }
            else
            {
                btny4.BackColor = Color.Blue;
            }
            #endregion

            #region
            if ((bool)siemens.Read("DB2.DBX0.4"))
            {
                btnm1.BackColor = Color.Red;
            }
            else
            {
                btnm1.BackColor = Color.Green;
            }
            if ((bool)siemens.Read("DB2.DBX0.4"))
            {
                btnm2.BackColor = Color.Red;
            }
            else
            {
                btnm2.BackColor = Color.Green;
            }
            if ((bool)siemens.Read("DB2.DBX0.4"))
            {
                btnm3.BackColor = Color.Red;
            }
            else
            {
                btnm3.BackColor = Color.Green;
            }
            if ((bool)siemens.Read("DB2.DBX0.4"))
            {
                btnm4.BackColor = Color.Red;
            }
            else
            {
                btnm4.BackColor = Color.Green;
            }
            #endregion

            #region
            labd1.Text = ((bool)siemens.Read("DB2.DBX0.4")).ToString();
            labd2.Text = ((bool)siemens.Read("DB2.DBX0.4")).ToString();
            labd3.Text = ((bool)siemens.Read("DB2.DBX0.4")).ToString();
            labd4.Text = ((bool)siemens.Read("DB2.DBX0.4")).ToString();
            #endregion

        }

        private void btny1_Click(object sender, EventArgs e)
        {
            if (btny1.BackColor == Color.Red)
            {
                siemens.Write("Q0.0", false);
            }
            else
            {
                siemens.Write("Q0.0", true);
            }
        }

        private void btny2_Click(object sender, EventArgs e)
        {
            if (btny2.BackColor == Color.Red)
            {
                siemens.Write("Q0.1", false);
            }
            else
            {
                siemens.Write("Q0.1", true);
            }
        }

        private void btny3_Click(object sender, EventArgs e)
        {
            if (btny3.BackColor == Color.Red)
            {
                siemens.Write("Q0.2", false);
            }
            else
            {
                siemens.Write("Q0.2", true);
            }
        }

        private void btny4_Click(object sender, EventArgs e)
        {
            if (btny4.BackColor == Color.Red)
            {
                siemens.Write("Q0.3", false);
            }
            else
            {
                siemens.Write("Q0.3", true);
            }
        }

        private void btnm1_Click(object sender, EventArgs e)
        {
            if (btnm1.BackColor == Color.Red)
            {
                siemens.Write("M1.0", false);
            }
            else
            {
                siemens.Write("M1.0", true);
            }
        }

        private void btnm2_Click(object sender, EventArgs e)
        {
            if (btnm2.BackColor == Color.Red)
            {
                siemens.Write("M1.1", false);
            }
            else
            {
                siemens.Write("M1.1", true);
            }
        }

        private void btnm3_Click(object sender, EventArgs e)
        {
            if (btnm3.BackColor == Color.Red)
            {
                siemens.Write("M1.2", false);
            }
            else
            {
                siemens.Write("M1.2", true);
            }
        }

        private void btnm4_Click(object sender, EventArgs e)
        {
            if (btnm4.BackColor == Color.Red)
            {
                siemens.Write("M1.3", false);
            }
            else
            {
                siemens.Write("M1.3", true);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Int16 sw = Convert.ToInt16(textBox1.Text);
            siemens.Write("MW100", sw);
        }
    }
}
